﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

namespace WebRole1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Service1 svc = new Service1();
            //svc.createNewGamePlay(1, 3, "20", "10", "1", "1", "1", "1", "1");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Service1 svc = new Service1();
            string result = uploadImage(FileUpload1.FileName, FileUpload1.FileBytes);
            lbl_image.Text = result;
        }

        /// <summary>
        /// test of upload image. It will be move to service.svc later
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="fileBytes"></param>
        /// <returns></returns>
        public string uploadImage(string fileName, byte[] fileBytes)
        {
            string result = "";
           
            CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
            {
                configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
            });
            // Get the storage account reference
            CloudStorageAccount objStorage = CloudStorageAccount.FromConfigurationSetting("BlobConnectionString");
            // get the Client reference using storage blobend point
            CloudBlobClient objClient = new CloudBlobClient(objStorage.BlobEndpoint, objStorage.Credentials);
            // Get Container reference
            CloudBlobContainer objContainer = objClient.GetContainerReference("mycontainer");
            // Get blob reference
            CloudBlob obj = objContainer.GetBlobReference(fileName);
            // Set meta values
            obj.Metadata["MetaName"] = "meta";
            // Open a stream using the cloud object
            BlobStream blobstream = obj.OpenWrite();
            // Write the stream to the blob database
            blobstream.Write(fileBytes, 0, fileBytes.Count());
            blobstream.Close();
            // Browse through blob list from the container
            IEnumerable<IListBlobItem> objBlobList = objContainer.ListBlobs();
            foreach (IListBlobItem objItem in objBlobList)
            {
                result += objItem.Uri + "<br>";
            }
            return result;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //Service1 svc = new Service1();
            //System.Drawing.Image objimg = svc.searchAndretrieveImage(txtSearch.Text);
            //// must be used to display the image
            //Response.Clear();
            //Response.ContentType = "image/gif";
            //objimg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            
        }
    }
}
